package com.hd.trans.dialog

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.hd.trans.R
import com.hd.trans.TranslationInterior
import com.hd.trans.databinding.DialogAiSettingBinding
import com.hd.trans.network.PreferenceMgr
import com.hd.trans.ui.base.TransInit
import com.hd.trans.utils.ToastUtils
import com.hd.trans.utils.TrackerForTrans

@SuppressLint("SetTextI18n")
class AiSettingDialog: DialogFragment(), View.OnClickListener {
    private lateinit var mDataBinding: DialogAiSettingBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设置dialog样式
        setStyle(STYLE_NO_TITLE, R.style.BottomDialogStyle)
    }

    override fun onStart() {
        super.onStart()
        dialog?.window!!.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        dialog?.window!!.setGravity(Gravity.BOTTOM)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        mDataBinding = DialogAiSettingBinding.inflate(inflater,container,false)

        dialog?.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) //设置背景为透明
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)

        initView()
        return mDataBinding.root
    }

    private fun initView() {
        mDataBinding.apply {
            mDataBinding.tvConfirm.setOnClickListener(this@AiSettingDialog)
            mDataBinding.tvCancel.setOnClickListener(this@AiSettingDialog)
            mDataBinding.etPrefix.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
                override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
                override fun afterTextChanged(s: Editable) {
                    updateResult()
                }
            })
            mDataBinding.etSuffix.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
                override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
                override fun afterTextChanged(s: Editable) {
                    updateResult()
                }
            })

            mDataBinding.etPrefix.setText(PreferenceMgr.getInstance().aiPrefix)
            mDataBinding.etSuffix.setText(PreferenceMgr.getInstance().aiSuffix)
        }
    }

    private fun updateResult() {
        if(getAiPrefix().trim().isNullOrEmpty() && getAiSuffix().trim().isNullOrEmpty()){
            mDataBinding.tvResult.text = ""
            return
        }
        mDataBinding.tvResult.text = "${getAiPrefix()}xxx${getAiSuffix()}"
    }

    private fun getAiPrefix(): String {
        return mDataBinding.etPrefix.text.toString()
    }

    private fun getAiSuffix(): String {
        return mDataBinding.etSuffix.text.toString()
    }

    override fun onClick(v: View) {
        when(v){
            mDataBinding.tvConfirm -> {
                ToastUtils.showNormal(getString(R.string.trans_set_success))
                PreferenceMgr.getInstance().aiPrefix = getAiPrefix()
                PreferenceMgr.getInstance().aiSuffix = getAiSuffix()
                dismiss()
            }
            mDataBinding.tvCancel -> {
                dismiss()
            }
        }
    }

    override fun show(manager: FragmentManager, tag: String?) {
        //视图埋点
        TranslationInterior.getTrackerListener().view(TrackerForTrans.VIEW_AI_SETTING)
        super.show(manager, tag)
    }
}